package org.itboys.product.service;

import org.itboys.admin.entity.PostFeeDB;
import org.itboys.baseobject.service.CityService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2016/6/13.
 */
@Service
public class GoodsSourcePostFeeService extends BaseService<PostFeeDB, Long> {
	@Autowired
	private CityService cityService;
    @Resource(name="adminDS")
    private MongoDataSource ds;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return ds;
    }

    @Override
    protected Class<PostFeeDB> getEntityClass() {
        return PostFeeDB.class;
    }

    public List<PostFeeDB> getAllSources() {
        return ds.createQuery(getEntityClass()).asList();
    }
    public int getExpressFee(int weight,Long sourceId,Long cityId){
        PostFeeDB postFeeDB=getMongoDataSource().createQuery(getEntityClass()).filter("goodsSourceId", sourceId).filter("districtId", cityId).filter("deleted", false).order("-ct").get();
		if (null==postFeeDB) {
			return 0;
		}
        Integer fee=postFeeDB.getFirstFee();
		Integer leftWeight=weight-postFeeDB.getFirstWeight();
		if (leftWeight<=0) {
			return fee;
		}else {
			fee+=postFeeDB.getAdditionalFee()*(Double.valueOf(Math.ceil(leftWeight/postFeeDB.getAdditionalWeight())).intValue());
		}
        return fee;
    }
}
